package com.neusoft.neuiotms.dcam.fixed.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.dto.InventoryDutyDTO;
import com.neusoft.neuiotms.dcam.fixed.service.InventoryDutyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "盘点任务管理")
@RestController
@RequestMapping(value = "/api/w/fixed/inventory/dutys")
public class InventoryDutyController {

    @Autowired
    private InventoryDutyService inventoryDutyService;

    @ApiOperation("查询所有盘点任务")
    @GetMapping
    public ResultListDTO<InventoryDutyDTO> index(final Searchable searchable) {
        final List<InventoryDutyDTO> DTOs = this.inventoryDutyService.findAll(searchable);
        final ResultListDTO<InventoryDutyDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有盘点任务")
    @GetMapping("/p")
    public PageResultDTO<InventoryDutyDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.inventoryDutyService.search(pageable, searchable));
    }

    @ApiOperation("创建盘点任务")
    @PostMapping
    public ResultDTO<InventoryDutyDTO> create(@RequestBody final InventoryDutyDTO dto) {
        final InventoryDutyDTO result = this.inventoryDutyService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("盘点任务详情")
    @GetMapping("/{id}")
    public ResultDTO<InventoryDutyDTO> details(@PathVariable final Long id) {
        final InventoryDutyDTO dto = this.inventoryDutyService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除盘点任务")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.inventoryDutyService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("修改盘点任务")
    @PutMapping("/{id}")
    public ResultDTO<InventoryDutyDTO> update(@PathVariable final Long id,
            @RequestBody final InventoryDutyDTO dto) {
        final InventoryDutyDTO result = this.inventoryDutyService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("确认任务状态")
    @PutMapping("/enable/{id}/status/{status}")
    public ResultDTO<Void> enable(@PathVariable final Long id, @PathVariable final String status) {
        this.inventoryDutyService.enable(id, status);
        return ResultDTO.success();
    }

    @ApiOperation("导出任务详情")
    @GetMapping(value = "/export/{id}")
    public void download(@PathVariable final Long id, final HttpServletResponse response) {

        final Workbook workbook = this.inventoryDutyService.download(id);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-disposition",
                    String.format("attachment; filename=%s",
                            URLEncoder.encode("任务详情" + LocalDateTime.now() + ".xlsx", "UTF-8")));
            workbook.write(out);
            out.flush();
        } catch (final Exception e) {
            InventoryDutyController.log.error("任务详情导出失败", e);
        } finally {
            try {
                out.close();
            } catch (final IOException e) {
                InventoryDutyController.log.error("任务详情导出关闭流失败", e);
            }
        }
    }

    @ApiOperation("导入盘点任务表")
    @PostMapping("/import/{id}")
    public ResultDTO<?> upload(@RequestParam("file") final MultipartFile file, @PathVariable final Long id) {
        final String msg = this.inventoryDutyService.upload(file, id);
        if (!StringUtils.isEmpty(msg)) {
            return ResultDTO.failure(msg);
        }
        return ResultDTO.success(msg);
    }
}
